//
//  TLHttpTool.m
//  Kaledai
//
//  Created by taolei-mac on 2017/5/15.
//  Copyright © 2017年 com.zhejiangchain.ios. All rights reserved.
//

#import "TLHttpTool.h"
#import "AFNetworking.h"
@implementation TLHttpTool

+ (AFHTTPRequestOperationManager *)createManager
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
    manager.requestSerializer.timeoutInterval = 30.0;
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    manager.securityPolicy.allowInvalidCertificates = YES;
    return manager;
}

+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    [self.createManager.requestSerializer setValue:[NSURL URLWithString:url].absoluteString forHTTPHeaderField:@"Referer"];
    // 2. 发送get请求
    [[self createManager] GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if(success){
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if(failure){
            failure(error);
        }
    }];
}

+ (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    [self.createManager.requestSerializer setValue:[NSURL URLWithString:url].absoluteString forHTTPHeaderField:@"Referer"];
    // 2. 发送get请求
    [[self createManager] POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if(success){
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if(failure){
            failure(error);
        }
    }];
}

@end
